package com.ucpaas.sms.action.componentConf;

import com.jsmsframework.common.dto.R;
import com.jsmsframework.middleware.entity.JsmsMonitorDetailOption;
import com.jsmsframework.middleware.exception.JsmsMonitorDetailOptionException;
import com.jsmsframework.middleware.service.JsmsMonitorDetailOptionService;
import com.jsmsframework.middleware.service.JsmsMonitorDetailOptionServiceImpl;
import com.ucpaas.sms.action.BaseAction;
import com.ucpaas.sms.util.CommonUtil;
import com.ucpaas.sms.util.rest.utils.DateUtil;
import com.ucpaas.sms.util.web.StrutsUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

/**
 * 组件配置,监控明细关联配置
 */
@Controller
@Scope("prototype")
@Results({@Result(name = "query", location = "/WEB-INF/content/componentConf/monitorDetailRefConf/query.jsp"),
        @Result(name = "edit", location = "/WEB-INF/content/componentConf/monitorDetailRefConf/edit.jsp")})
public class MonitorDetailRefConfAction extends BaseAction {

    private static final Logger LOGGER = LoggerFactory.getLogger(JsmsMonitorDetailOptionServiceImpl.class);

    @Autowired
    private JsmsMonitorDetailOptionService jsmsMonitorDetailOptionService;

    @Action("/componentConf/monitorDetailRefConf/query")
    public String query() {
        Map<String, String> params = StrutsUtils.getFormData();
        Map<String, Object> objectMap = new HashMap<>();

        Object obj = new Object();

        //列名
        obj = params.get("fieldName");
        if (obj != null) {
            objectMap.put("fieldName", obj);
        }
        //表名
        obj = params.get("measurement");
        if (obj != null) {
            objectMap.put("measurement", obj);
        }
        //状态
        obj = params.get("state");
        if (obj != null) {
            objectMap.put("state", obj);
        }

        jpage = CommonUtil.initJsmsPage(params);
        jpage.setParams(objectMap);
        jpage.setOrderByClause("update_date DESC");
        jpage = jsmsMonitorDetailOptionService.queryList(jpage);

        StrutsUtils.setAttribute("fieldName_param", params.get("fieldName"));
        StrutsUtils.setAttribute("measurement_param", params.get("measurement"));
        StrutsUtils.setAttribute("state_param", params.get("state"));

        return "query";
    }

    @Action("/componentConf/monitorDetailRefConf/add")
    public String add() {
        return "edit";
    }

    @Action("/componentConf/monitorDetailRefConf/save")
    public void updateAndSaveMonitorDetailRef() {
        Map<String, String> params = StrutsUtils.getFormData();
        R r = new R();
        //修改或新增标识
        boolean isMod = false;
        try {

            JsmsMonitorDetailOption jsmsMonitorDetailOption = new JsmsMonitorDetailOption();

            //id
            String id = params.get("id");
            if (StringUtils.isNotBlank(id)) {
                isMod = true;
                jsmsMonitorDetailOption.setId(Integer.parseInt(id));
            }
            //列名
            String fieldName = params.get("fieldName");
            if (StringUtils.isNotBlank(fieldName)) {
                jsmsMonitorDetailOption.setFieldName(fieldName);
            }
            //类型
            String fieldType = params.get("fieldType");
            if (StringUtils.isNotBlank(fieldType)) {
                int fieldInteger = Integer.parseInt(fieldType);
                jsmsMonitorDetailOption.setFieldType(fieldInteger);
            }
            //所属表名
            String measurement = params.get("measurement");
            if (StringUtils.isNotBlank(measurement)) {
                int measurementInteger = Integer.parseInt(measurement);
                jsmsMonitorDetailOption.setMeasurement(measurementInteger);
            }
            //状态
            String state = params.get("state");
            if (StringUtils.isNotBlank(state)) {
                int stateInteger = Integer.parseInt(state);
                jsmsMonitorDetailOption.setState(stateInteger);
            }
            //描述
            String remark = params.get("remark");
            if (StringUtils.isNotBlank(remark)) {
                jsmsMonitorDetailOption.setRemark(remark);
            }

            if (isMod) {
                //更新时间
                jsmsMonitorDetailOption.setUpdateDate(DateUtil.getCurrentDate());
                int update = jsmsMonitorDetailOptionService.updateOption(jsmsMonitorDetailOption);
                if (update != 1) {
                    throw new JsmsMonitorDetailOptionException("更新监控关联配置失败");
                }
            } else {
                int insert = jsmsMonitorDetailOptionService.add(jsmsMonitorDetailOption);
                if (insert != 1) {
                    throw new JsmsMonitorDetailOptionException("新增监控关联配置失败");
                }
            }

            r.setCode(200);
            r.setMsg("操作成功");
        } catch (JsmsMonitorDetailOptionException e) {
            LOGGER.debug("更新保存监控明细关联配置信息失败", e);
            r.setCode(500);
            r.setMsg(e.getMessage());
        } catch (Exception e) {
            LOGGER.debug("更新保存监控明细关联配置信息失败", e);
            r.setCode(500);
            r.setMsg("操作异常");

        }
        StrutsUtils.renderJson(r);
    }

    @Action("/componentConf/monitorDetailRefConf/edit")
    public String edit() {
        Map<String, String> params = StrutsUtils.getFormData();
        Map<String, Object> data1 = new HashMap<>();

        try {
            String id = params.get("id");
            if (StringUtils.isBlank(id)) {
                throw new JsmsMonitorDetailOptionException("id不能为空");
            }

            JsmsMonitorDetailOption jsmsMonitorDetailOption = jsmsMonitorDetailOptionService.getById(Integer.parseInt(id));
            if (jsmsMonitorDetailOption != null) {
                data1.put("id", jsmsMonitorDetailOption.getId());
                data1.put("fieldName", jsmsMonitorDetailOption.getFieldName());
                data1.put("fieldType", jsmsMonitorDetailOption.getFieldType());
                data1.put("measurement", jsmsMonitorDetailOption.getMeasurement());
                data1.put("state", jsmsMonitorDetailOption.getState());
                data1.put("remark", jsmsMonitorDetailOption.getRemark());
                data = data1;
            }
        } catch (JsmsMonitorDetailOptionException e) {
            LOGGER.debug("编辑监控明细关联配置信息失败", e);
        } catch (NumberFormatException e) {
            LOGGER.debug("编辑监控明细关联配置信息失败", e);
        }
        return "edit";
    }

    @Action("/componentConf/monitorDetailRefConf/delete")
    public void delete() {
        Map<String, String> params = StrutsUtils.getFormData();
        R r = new R();

        try {
            String id = params.get("id");
            if (StringUtils.isNotBlank(id)) {
                Map<String, Object> deleteMap = new HashMap<>();
                deleteMap.put("id", id);

                int parseInt = Integer.parseInt(id);
                JsmsMonitorDetailOption jsmsMonitorDetailOption = jsmsMonitorDetailOptionService.getById(parseInt);
                if (jsmsMonitorDetailOption != null) {
                    jsmsMonitorDetailOptionService.delete(deleteMap);
                } else {
                    throw new JsmsMonitorDetailOptionException("无此id的相关信息");
                }
            } else {
                throw new JsmsMonitorDetailOptionException("id不能为空");
            }

            r.setCode(200);
            r.setMsg("删除成功");
        } catch (JsmsMonitorDetailOptionException e) {
            LOGGER.debug("删除监控明细关联配置信息失败", e);
            r.setCode(500);
            r.setMsg(e.getMessage());
        } catch (Exception e) {
            LOGGER.debug("删除监控明细关联配置信息失败", e);
            r.setCode(500);
            r.setMsg("操作异常");
        }
        StrutsUtils.renderJson(r);
    }

}
